﻿using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.GuideData;
using BonjourQuotation.UI.Models.AccountModel;
using BonjourQuotation.UI.Models.GuideModel;
using BonjourQuotation.UI.Models.PartialModel;
using BonjourQuotation.UI.Models.ReportModel;
using BonjourQuotation.UI.Models.Shared;
using BonjourQuotation.UI.ServiceManagers;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;


namespace BonjourQuotation.UI.Controllers
{
    public class GuideController : BaseController
    {
        GuideManager GuideManager;
        [Authorize]
        [HttpGet]
        public ActionResult ManageGuides()
        {
            GuideManager = new GuideManager();
            var ManageGuidesData = GuideManager.LoadGuides(1);
            ModelState.Clear();
            var ManageGuidesViewModel = InitManageGuidesSearchParams(ManageGuidesData);
            this.SetPermission(ManageGuidesViewModel, AllPermVModel);
            return View("ManageGuides", ManageGuidesViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadGuidesGrid(ManageGuidesGridViewModel model)
        {
            GuideManager = new GuideManager();
            ManageGuidesGridBData ManageGuidesData = new ManageGuidesGridBData();
            if (model.IsSearch)
            {
                ManageGuidesGridBData SearchGuidesData = new ManageGuidesGridBData();
                SearchGuidesData = InitSearchGuide(model);
                ManageGuidesData = GuideManager.LoadSearchedGuides(SearchGuidesData);
            }
            else
                ManageGuidesData = GuideManager.LoadGuides(model.PageNum);
            ModelState.Clear();
            var ManageGuidesViewModel = InitManageGuidesVModel(ManageGuidesData, model.PageNum);
            this.SetPermission(ManageGuidesViewModel, AllPermVModel);
            return PartialView("LoadGuidesGrid", ManageGuidesViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadGuideCompanyInfo(int GuideID)
        {
            GuideManager = new GuideManager();
            var GuideDetData = GuideManager.LoadGuideCompanyInfoByID(GuideID);
            ModelState.Clear();
            var GuideDetViewModel = InitGuideCompanyInfoViewModel(GuideDetData);
            this.SetPermission(GuideDetViewModel, AllPermVModel);
            if (GuideID == 0)
                GuideDetViewModel.Active = true;
            return PartialView("LoadGuideCompanyInfoGrid", GuideDetViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadGuidePersonalInfo(int GuideID)
        {
            GuideManager = new GuideManager();
            var GuideDetData = GuideManager.LoadGuidePersonalInfoByID(GuideID);
            ModelState.Clear();
            var GuideDetViewModel = InitGuidePersonalInfoViewModel(GuideDetData);
            this.SetPermission(GuideDetViewModel, AllPermVModel);
            return PartialView("LoadGuidePersonalInfoGrid", GuideDetViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadGuideAssignments(int GuideID)
        {
            GuideManager = new GuideManager();
            var GuideAssData = GuideManager.LoadGuideAssignmentsByID(GuideID);
            ModelState.Clear();
            return PartialView("LoadGuideAssignmentsGrid", InitGuideAssignmentsViewModel(GuideAssData));
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveGuideCompanyInfoData(ManageGuideCompanyInfoViewModel model)
        {
            GuideManager = new GuideManager();
            var GuideData = InitGuideCompanyInfoBData(model);
            if (model.GuideID == 0)
            {
                int GuideID = 0;
                var CreateError = GuideManager.CreateGuideCompanyInfo(GuideData, ref GuideID);
                MessageBoxViewModel MessBoxforCreate;
                if (CreateError.Message != null)
                {
                    MessBoxforCreate = new MessageBoxViewModel()
                    {
                        Message = "Error - " + CreateError.Message,
                    };
                    return PartialView("MessageBox", MessBoxforCreate);
                }
                var GuideDetData = GuideManager.LoadGuideCompanyInfoByID(GuideID);
                ModelState.Clear(); 
                return PartialView("LoadGuideCompanyInfoGrid", InitGuideCompanyInfoViewModel(GuideDetData));
            }
            var EditError = GuideManager.SaveGuideCompanyInfoData(GuideData);
            if (EditError.Message != null)
            {
                MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = "Error - " + EditError.Message,
                };
                return PartialView("MessageBox", MessBoxforEdit);
            }
            var GuideDetDataEdit = GuideManager.LoadGuideCompanyInfoByID(model.GuideID);
            ModelState.Clear();
            return PartialView("LoadGuideCompanyInfoGrid", InitGuideCompanyInfoViewModel(GuideDetDataEdit));
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveGuidePersonalInfoData(ManageGuidePersonalInfoViewModel model)
        {
            GuideManager = new GuideManager();
            var GuideData = InitGuidePersonalInfoBData(model);
            if (model.GuideID == 0)
            {
                int GuideID = 0;
                var CreateError = GuideManager.CreateGuidePersonalInfo(GuideData, ref GuideID);
                MessageBoxViewModel MessBoxforCreate;
                if (CreateError.Message != null)
                {
                    MessBoxforCreate = new MessageBoxViewModel()
                    {
                        Message = "Error - " + CreateError.Message,
                    };
                    return PartialView("MessageBox", MessBoxforCreate);
                }
                var GuideDetData = GuideManager.LoadGuidePersonalInfoByID(GuideID);
                ModelState.Clear(); 
                return PartialView("LoadGuidePersonalInfoGrid", InitGuidePersonalInfoViewModel(GuideDetData));
            }
            var EditError = GuideManager.SaveGuidePersonalInfoData(GuideData);
            if (EditError.Message != null)
            {
                MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
                {
                    Message = "Error - " + EditError.Message,
                };
                return PartialView("MessageBox", MessBoxforEdit);
            }
            var GuideDetDataEdit = GuideManager.LoadGuidePersonalInfoByID(model.GuideID);
            ModelState.Clear();
            return PartialView("LoadGuidePersonalInfoGrid", InitGuidePersonalInfoViewModel(GuideDetDataEdit));
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetStateByCountryID(int CountryID)
        {
            GuideManager = new GuideManager();
            var data = GuideManager.LoadState();
            List<System.Web.WebPages.Html.SelectListItem> StateViewModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var state in data)
            {
                StateViewModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = state.Value, Value = state.Key });

            }
            return Json(StateViewModel);
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetCityByCountryID(int CountryID, string StateID)
        {
            GuideManager = new GuideManager();
            var data = GuideManager.LoadCity(CountryID, StateID);
            List<System.Web.WebPages.Html.SelectListItem> CityViewModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var city in data)
            {
                CityViewModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });

            }
            return Json(CityViewModel);
        }
        [Authorize]
        public PartialViewResult AutoComplete(string PropName, string keyword)
        {
            QuotationManager QuotaManager = new QuotationManager();
            var data = QuotaManager.FindKeywordInEntity(PropName, keyword);
            AutoCompleteViewModel AutoCompVMode = new AutoCompleteViewModel() { Items = data };
            return PartialView("QuotaAutoComplete", AutoCompVMode);
        }
        #region private methods
        private ManageGuidesGridViewModel InitManageGuidesVModel(ManageGuidesGridBData ManageGuidesData, int PageNum)
        {
            ManageGuidesGridViewModel ManGuidesVModel = new ManageGuidesGridViewModel();
            ManGuidesVModel.Rows = new List<ManageGuidesGridRowViewModel>();
            foreach (var Guide in ManageGuidesData.Rows)
                ManGuidesVModel.Rows.Add(new ManageGuidesGridRowViewModel
                {
                    GuideID = Guide.GuideID,
                    GuideName = Guide.GuideName,
                    GuideLastName = Guide.GuideLastName,
                    City = Guide.City,
                    IsActive = Guide.Active == null ? false : Convert.ToBoolean(Guide.Active),
                    IsIsFind = Guide.IsFind == null ? false : Convert.ToBoolean(Guide.IsFind),
                });
            if (ManGuidesVModel.Rows.Count == ManageGuidesGridBData.PageCount * PageNum)
                ManGuidesVModel.IsCanView = true;
            else
                ManGuidesVModel.IsCanView = false;
            return ManGuidesVModel;
        }
        private ManageGuidesGridViewModel InitManageGuidesSearchParams(ManageGuidesGridBData ManageGuidesData)
        {
            ManageGuidesGridViewModel ManGuidesVModel = new ManageGuidesGridViewModel();

            ManGuidesVModel.CitySearchText = "";
            ManGuidesVModel.CitySearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in ManageGuidesData.City)
                ManGuidesVModel.CitySearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Value });
            ManGuidesVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in ManageGuidesData.Active)
                ManGuidesVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ManGuidesVModel.IsFindSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var IsFind in ManageGuidesData.IsFind)
                ManGuidesVModel.IsFindSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = IsFind.Value, Value = IsFind.Key.ToString() });
            return ManGuidesVModel;
        }
        private ManageGuideCompanyInfoViewModel InitGuideCompanyInfoViewModel(GuideCompanyInfoBData data)
        {
            ManageGuideCompanyInfoViewModel GuideDetVModel = new ManageGuideCompanyInfoViewModel
            {
                GuideID = data.GuideID,
                FirstName = data.FirstName,
                MiddleName = data.MiddleName,
                LastName = data.LastName,
                DateHired = data.DateHired != null ? Convert.ToDateTime(data.DateHired).ToString("dd/MMM/yy") : "",
                Active = data.Active == null ? false : Convert.ToBoolean(data.Active),
                IsFind = data.IsFind == null ? false : Convert.ToBoolean(data.IsFind),
                Phone = data.Phone,
                Fax = data.Fax,
                SelectedLCountryID = data.SelectedLCountryID.ToString(),
                SelectedRegionId = data.SelectedRegionID.ToString(),
                SelectedAreaId = data.SelectedAreaID.ToString(),
                Note = data.Note,
                Salary = data.Salary != null ? Convert.ToDecimal(data.Salary).ToString("F") : "0",

            };
            GuideDetVModel.RegionId = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Reg in data.RegionID)
                GuideDetVModel.RegionId.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Reg.Value, Value = Reg.Key.ToString() });
            GuideDetVModel.AreaId = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Area in data.AreaID)
                GuideDetVModel.AreaId.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Area.Value, Value = Area.Key.ToString() });
            return GuideDetVModel;
        }
        private ManageGuidePersonalInfoViewModel InitGuidePersonalInfoViewModel(GuidePersonalInfoBData data)
        {
            ManageGuidePersonalInfoViewModel GuideDetVModel = new ManageGuidePersonalInfoViewModel
            {
                GuideID = data.GuideID,
                SocialSecurityNumber = data.SocialSecurityNumber,
                SelectedSex = data.SelectedSex != null ? Convert.ToByte(data.SelectedSex).ToString() : null,
                Address = data.Address,
                SelectedCity = data.SelectedCity,
                CityID = data.CityID.ToString(),
                SelectedStateID = data.SelectedStateID,
                SelectedCountry = data.SelectedCountryID.ToString(),
                SelectedLCountryID = data.SelectedLCountryID.ToString(),
                PostalCode = data.PostalCode,
                BirthDate = data.BirthDate != null ? Convert.ToDateTime(data.BirthDate).ToString("dd/MMM/yy") : "",
                Email = data.EMail,
                SpouseName = data.SpouseName,
                EmrgcyContactName = data.EmrgcyContactName,
                EmrgcyContactPhone = data.EmrgcyContactPhone,
            };
            GuideDetVModel.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var stID in data.StateID)
                GuideDetVModel.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = stID.Value, Value = stID.Key });
            GuideDetVModel.Country = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in data.CountryID)
                GuideDetVModel.Country.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            GuideDetVModel.LCountry = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in data.LCountryID)
                GuideDetVModel.LCountry.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            GuideDetVModel.City = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in data.City)
                GuideDetVModel.City.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });
            GuideDetVModel.Sex = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Sex in data.Sex)
                GuideDetVModel.Sex.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Sex.Value, Value = Sex.Key.ToString() });
            return GuideDetVModel;
        }
        private ManageGuidesGridBData InitSearchGuide(ManageGuidesGridViewModel Data)
        {
            ManageGuidesGridBData SrchData = new ManageGuidesGridBData
            {
                GuideName = Data.GuideNameText,
                GuideLastName = Data.GuideLastNameText,
                SelectedCity = Data.CitySearchText,
                SelectedActive = Data.IsActiveSearch,
                SelectedIsFind = Data.IsIsFindSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private GuideCompanyInfoBData InitGuideCompanyInfoBData(ManageGuideCompanyInfoViewModel data)
        {
            GuideCompanyInfoBData GuideBData = new GuideCompanyInfoBData
            {
                GuideID = data.GuideID,
                FirstName = data.FirstName,
                MiddleName = data.MiddleName,
                LastName = data.LastName,
                DateHired = data.DateHired != null ? Convert.ToDateTime(data.DateHired) : (DateTime?)null,
                Active = data.Active,
                IsFind = data.IsFind,
                Phone = data.Phone,
                Fax = data.Fax,
                SelectedRegionID = data.SelectedRegionId != null ? Convert.ToInt16(data.SelectedRegionId) : (int?)null,
                SelectedAreaID = data.SelectedAreaId != null ? Convert.ToInt16(data.SelectedAreaId) : (int?)null,
                Note = data.Note,
                Salary = data.Salary != null ? Convert.ToDecimal(data.Salary.Replace(",","")) : (decimal?)0.00,
            };
            return GuideBData;
        }
        private GuidePersonalInfoBData InitGuidePersonalInfoBData(ManageGuidePersonalInfoViewModel data)
        {
            GuidePersonalInfoBData GuideBData = new GuidePersonalInfoBData
            {
                GuideID = data.GuideID,
                SocialSecurityNumber = data.SocialSecurityNumber,
                SelectedSex = data.SelectedSex != null ? Convert.ToByte(data.SelectedSex) : (byte?)null,
                Address = data.Address,
                CityID = (data.CityID != null && data.CityID != "0") ? Convert.ToInt32(data.CityID) : (int?)null,
                SelectedStateID = data.SelectedLCountryID == "1" ? (data.SelectedStateID == "0" ? null : data.SelectedStateID) : null,
                SelectedCountryID = data.SelectedCountry != null ? Convert.ToInt32(data.SelectedCountry) : (int?)null,
                SelectedLCountryID = data.SelectedLCountryID != null ? Convert.ToInt32(data.SelectedLCountryID) : (int?)null,
                PostalCode = data.PostalCode,
                BirthDate = data.BirthDate != null ? Convert.ToDateTime(data.BirthDate) : (DateTime?)null,
                EMail = data.Email,
                SpouseName = data.SpouseName,
                EmrgcyContactName = data.EmrgcyContactName,
                EmrgcyContactPhone = data.EmrgcyContactPhone,
            };
            return GuideBData;
        }
        private ManageGuideAssignmentsGridViewModel InitGuideAssignmentsViewModel(GuideAssignmentsBData data)
        {
            ManageGuideAssignmentsGridViewModel InitAssig = new ManageGuideAssignmentsGridViewModel();
            InitAssig.Rows = new List<GuideAssignmentsViewModel>();
            foreach (var rows in data.Rows)
            {
                InitAssig.Rows.Add(new GuideAssignmentsViewModel()
                {
                    StartDate = rows.StartDate != null ? Convert.ToDateTime(rows.StartDate).ToString("dd/MMM/yy") : "",
                    EndDate = rows.FinishDate != null ? Convert.ToDateTime(rows.FinishDate).ToString("dd/MMM/yy") : "",
                    CustomerCode = rows.CustomerCode,
                    GroupCode = rows.GroupCode,
                    GroupName = rows.GroupName,
                    Canceled = rows.Canceled == null ? false : Convert.ToBoolean(rows.Canceled),
                    Selected = rows.Selected == null ? false : Convert.ToBoolean(rows.Selected),
                });
            }
            return InitAssig;
        }
        #endregion
    }
}